package com.zzyl.controller;

import cn.hutool.core.bean.BeanException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
@Log4j2
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }
    /**
     * 新增床位
     */
    @PostMapping("/create")
    @ApiOperation(value = "新增床位", notes = "传入床位信息")
    @ApiParam(value = "床位信息", required = true)
    @ApiOperationSupport(includeParameters = {"bedDto.bedNumber", "dedDto.sort", "bedDto.roomId"})
    public ResponseResult createBed(
           @RequestBody BedDto bedDto){
        log.info("新增床位：{}",bedDto);
        try {
            bedService.createBed(bedDto);
            return ResponseResult.success();
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }

    }

    /**
     * 删除床位
     */
    @ApiOperation(value = "删除床位i", notes = "传入床位id")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteBed(
            @ApiParam(value = "床位id", required = true)@PathVariable("id") Integer id){
        bedService.deleteBed(id);
        return ResponseResult.success();
    }

    /**
     * 修改床位
     */
    @ApiOperation(value = "修改床位", notes = "传入床位信息")
    @PutMapping("/update")
    public ResponseResult updateBed(
            @ApiParam(value = "床位信息", required = true)@RequestBody BedDto bedDto){
        bedService.updateBed(bedDto);
        return ResponseResult.success();
    }
    @GetMapping("/read/{id}")
    @ApiOperation(value = "根据床位id查询床位信息", notes = "传入床位id")
    @ApiParam(value = "床位id", required = true)
    public ResponseResult readBed(@PathVariable("id") Integer id){
         Bed bed = bedService.getById(id);
        return ResponseResult.success(bed);
    }
}
